package org.coding.springbootdemocommandline.fileopr.service;

import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;

/**
 * ^ enjoy coding and be bugfree boy
 *
 * @author guoxh
 * @date 2020/11/5 9:17 下午
 * @desc
 **/
@Service
public class FileOperationService {
    //BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);

    public Graphics2D initPainter(BufferedImage image) {
        Graphics2D g2Painter = image.createGraphics();
        //给painter带上反锯齿属性，否则生成图片会像被蚂蚁啃过
        g2Painter.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2Painter.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        return g2Painter;
    }

    public void operate() {
        int x = 40, y = 40, border = 1;
        BufferedImage avatarImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_ARGB);
        Graphics2D avatarPainter = initPainter(avatarImage); //如步骤1

        //切割头像区域为圆形
        Ellipse2D.Double shape = new Ellipse2D.Double(border, border,
                x - border * 2, y - border * 2);
        avatarPainter.setClip(shape);



        BufferedImage avatar;
        try {
            String path = ResourceUtils.getURL("classpath:").getPath()+"static/test.png";
            File file = new File(path); //要导入头像的url，自行替换
            avatar = ImageIO.read(file.toURL());

            //画头像
            avatarPainter.drawImage(avatar, border, border, x - border * 2, y - border * 2, null);
            //画边框
            Stroke s = new BasicStroke(1F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
            avatarPainter.setStroke(s);
            avatarPainter.setColor(Color.white);
            avatarPainter.drawOval(border, border, x - border * 2, y - border * 2);



            saveImage(avatarImage,ResourceUtils.getURL("classpath:").getPath()+"/static/"+System.currentTimeMillis()+".png");

            //释放图像处理资源
            avatarPainter.dispose();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static Boolean saveImage(BufferedImage productImage, String path){
        try{
            File outputFile = new File(path);
            if(!outputFile.exists()){
                outputFile.getParentFile().mkdirs();
                Boolean isSuccess = outputFile.createNewFile();
                if(!isSuccess){
                    return false;
                }
            }

//            Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
//            ImageWriter writer = iter.next();
//            ImageWriteParam iwp = writer.getDefaultWriteParam();
//            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
//            iwp.setCompressionQuality(1.0F);

//            writer.setOutput(ImageIO.createImageOutputStream(outputFile));
//            writer.write(null, new IIOImage(productImage ,null,null), iwp);
//            writer.dispose();
            //File outputfile  = new File("save.png");
            ImageIO.write(productImage,"png",outputFile);
            return true;
        }catch (IOException e) {
            e.printStackTrace();
            return false;

        }
    }

        }
